<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>欢迎使用校园二手交易系统</title>

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link
            href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
            rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

</head>
<script type="text/javascript" src="../js/jquery.min.js" ></script>
<script>
</script>

<body onload="init()">

<div id="wrapper">

    <!-- Sidebar -->
    <ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">

        <a class="sidebar-brand d-flex align-items-center justify-content-center" href="main.html">
            <div class="sidebar-brand-icon rotate-n-15">
                <i class="fas fa-laptop-house"></i>
            </div>
            <div class="sidebar-brand-text mx-3" style="font-size: 19px;">校园二手交易</div>
        </a>

        <hr class="sidebar-divider">

        <!-- 管理员功能 -->
        <div class="sidebar-heading" style="font-size: 16px;">
            管理员功能
        </div>

        <li class="nav-item">
            <a class="nav-link" href="adminGoodsList.html">
                <i class="fas fa-fw fa-newspaper"></i>
                <span style="font-size: 20px;">商品信息</span>
            </a>
        </li>

        <li class="nav-item">
            <a class="nav-link" href="adminUserList.html">
                <i class="fas fa-fw fa-user"></i>
                <span style="font-size: 20px;">用户信息</span></a>
        </li>

        <li class="nav-item">
            <a class="nav-link" href="adminCityList.html">
                <i class="fas fa-fw fa-city"></i>
                <span style="font-size: 20px;">城市信息</span></a>
        </li>

        <li class="nav-item active">
            <a class="nav-link" href="adminSchoolList.html">
                <i style="font-size: 18px"  class="fas fa-fw fa-check-circle"></i>
                <span style="font-size: 24px; font-weight: bold">学校信息</span>
            </a>
        </li>

        <!-- Divider -->
        <hr class="sidebar-divider d-none d-md-block">

        <!-- Sidebar Toggler (Sidebar) -->
        <div class="text-center d-none d-md-inline">
            <button class="rounded-circle border-0" id="sidebarToggle"></button>
        </div>

    </ul>
    <!-- End of Sidebar -->

    <!-- Content Wrapper -->
    <div id="content-wrapper" class="d-flex flex-column">

        <!-- Main Content -->
        <div id="content">

            <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
                <div style="margin-left: 15px">
                    点击相应标题查看对应信息
                </div>
                <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                    <i class="fa fa-bars"></i>
                </button>

                <!-- Topbar Navbar -->
                <ul class="navbar-nav ml-auto">

                    <!--                        <div class="topbar-divider d-none d-sm-block"></div>-->

                    <!-- Nav Item - User Information -->
                    <li class="nav-item dropdown no-arrow">
                        <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
                           data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <span class="mr-2 d-none d-lg-inline text-gray-600 small">
                                        欢迎您，
                                        <input type="text" id="wecomeName" style="width: 80px; border-style: none" disabled>
                                </span>
                            <img class="img-profile rounded-circle" id="sculpture"
                                 src="img/undraw_profile.svg">
                        </a>
                        <!-- Dropdown - User Information -->
                        <div class="dropdown-menu dropdown-menu-right shadow animated--grow-in"
                             aria-labelledby="userDropdown">
                            <a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">
                                <i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>
                                退出登录
                            </a>
                        </div>
                    </li>

                </ul>

            </nav>

            <form id="form1" onsubmit="return false" action="##" style="margin-left: 20px;margin-bottom: 20px">
                <table style="margin: auto;height:180px; width: 600px" class="table table-bordered border-0">
                    <tr>
                        <th>
                            <div style="display: flex; justify-content: center; align-items: center" class="input-group">
                                搜索：&ensp;<input type="text" name="keyword" id="keyword" class="form-control bg-light border-1 small" placeholder="请输入用户名关键词..."
                                                aria-label="Search" aria-describedby="basic-addon2">
                                <div class="input-group-append">
                                    <button class="btn btn-primary" type="button" onclick="init()">
                                        <i class="fas fa-search fa-fw"></i>
                                    </button>
                                </div>
                            </div>
                            <br>
                            选择城市：&ensp;&ensp;&ensp;&ensp;<select id='cityList' onclick="init()"> <option value=''>请选择</option> </select>
                        </th>
                    </tr>
                </table>
            </form>

            <div style="display: flex; justify-content: center; align-items: center">
                学校名称：&ensp;&ensp;<input id=insertName style='text-align: center'/>&ensp;&ensp;
                <select id='insertCityId' > <option value=''>请选择</option> </select>&ensp;&ensp;
                <a href="javascript:insert()" style='text-align: center' class='btn btn-primary'>创建新学校</a>
            </div>
            <table  style="height:400px;margin-bottom: 0px;" class="table table-bordered">
                <thead>
                <tr>
                    <th style="width: 10%">学校id</th>
                    <th style="width: 10%">学校名称</th>
                    <th style="width: 10%">所在城市</th>
                    <th style="width: 10%">修改</th>
                    <th style="width: 10%">删除</th>
                </tr>
                </thead>
                <tbody id="t_body">
                </tbody>
            </table>

        </div>

        <div class="copyright text-center my-auto">
            <button class="btn-secondary" onclick="prePage()">上一页</button>
            <input type="hidden" id="prePage"/>
            <input type="text" value="1" id="pageNum" style="width: 30px; border-style: none;text-align:center" disabled/>
            <button class="btn-secondary" onclick="nextPage()">下一页</button>
            <input type="hidden" id="nextPage"/>
        </div>

        <footer class="sticky-footer bg-white">
            <div class="container my-auto">
                <div class="copyright text-center my-auto">
                    <span>致 力 于 打 造 最 方 便 安 全 的 校 园 二 手 交 易 服 务</span>
                </div>
            </div>
        </footer>

    </div>

</div>

<!-- Logout Modal-->
<div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
     aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">确定要退出登录？</h5>
                <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">点击‘退出’后，退出系统...</div>
            <div class="modal-footer">
                <button class="btn btn-secondary" type="button" data-dismiss="modal">取消</button>
                <a class="btn btn-primary" href="login.html">退出</a>
            </div>
        </div>
    </div>
</div>

<!-- Bootstrap core JavaScript-->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- Core plugin JavaScript-->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>

<!-- Custom scripts for all pages-->
<script src="js/sb-admin-2.min.js"></script>

<!-- Page level plugins -->
<script src="vendor/chart.js/Chart.min.js"></script>

<!-- Page level custom scripts -->
<script src="js/demo/chart-area-demo.js"></script>
<script src="js/demo/chart-pie-demo.js"></script>


</body>
<script>
    userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
    if(userInfo.userName != null){
        document.getElementById('wecomeName').value = userInfo.userName; // 设置用户名
    }
    $("#sculpture").attr("src", userInfo.photo);  // 设置头像

    function prePage() {
        if(document.getElementById('prePage').value == 0){
            alert("当前为第一页");
            return;
        }
        document.getElementById('pageNum').value = document.getElementById('prePage').value;
        init();
    }
    function nextPage() {
        if(document.getElementById('nextPage').value == 0){
            alert("当前为最后一页");
            return;
        }
        document.getElementById('pageNum').value = document.getElementById('nextPage').value;
        init();
    }
    function init() {
        userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
        var pageCityParams = {
            pageNum: $("#pageNum").val(),
            schoolName: $("#keyword").val(),
            cityId: $("#cityList").val()
        }
        $.ajax({
            type : "POST",
            async: false,
            url : "user/user-server/pageSchool",
            headers: { // token
                'Authorization': userInfo.token
            },
            dataType: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(pageCityParams),
            success : function(result) {
                if (result.status == 200) {
                    document.getElementById('pageNum').value = result.data.pageNum; // 设置页数
                    document.getElementById('prePage').value = result.data.prePage;
                    document.getElementById('nextPage').value = result.data.nextPage;
                    var str = "";
                    var data = result.data.list;
                    for (var i = 0; i < data.length; i++) {
                        str += "<tr>" +
                            "<td style='vertical-align: middle'>" + data[i].id + "</a></td>" +
                            "<td style='vertical-align: middle'>" + data[i].schoolName + "</td>" +
                            "<td style='vertical-align: middle'>" + data[i].cityName + "</td>" +
                            "<td style='vertical-align: middle'><a href=\"javascript:updateName(\'" + data[i].id + "',\'" + data[i].schoolName + "',\'" + data[i].cityId + "')\" style='text-align: center' class='btn btn-success'>修改</a></td>" +
                            "<td style='vertical-align: middle'><a href=\"javascript:deleteById(\'" + data[i].id + "')\" style='text-align: center' class='btn btn-danger'>删除</a></td>";
                        str += "</tr>";
                    }
                    $("#t_body").html("");
                    $("#t_body").append(str);
                } else{
                    alert(result.msg);
                }
            }
        })
        $.ajax({ // 获取城市列表
            type : "GET",
            async: true,
            url : "user/user-server/listCity",
            headers: { // token
                'Authorization' : userInfo.token
            },
            success : function(result) {
                if (result!=null) {
                    var data = result.data;
                    var sel = document.getElementById("insertCityId");//获取下拉选对象
                    var cityList = document.getElementById("cityList");//获取下拉选对象
                    sel.options.length = 1; //清空
                    cityList.options.length = 1; //清空
                    for(var i=0,l=data.length;i<l;i++){
                        var name = data[i].cityName;
                        var id = data[i].id;
                        var option = new Option(name,id);
                        sel.options.add(option);
                        var option2 = new Option(name,id);
                        cityList.options.add(option2);
                    }
                    return;
                }
            }
        })
    }

    function updateName(id, schoolName, cityId) {
        var str = "";
        str += "<tr>" +
            "<td style='vertical-align: middle'>" + id + "</td>" +
            "<td style='vertical-align: middle'><input id=newName value=\'" + schoolName + "')\" style='text-align: center'/></td>" +
            "<td style='vertical-align: middle'><select id='cityId' > <option value=''>请选择</option> </select></td>" +
            "<td style='vertical-align: middle'><a href=\"javascript:update(\'" + id + "')\" style='text-align: center' class='btn btn-success'>修改</a></td>" +
            "<td style='vertical-align: middle'><a href=\"javascript:init()\" style='text-align: center' class='btn btn-google'>返回</a></td>";
        str += "</tr>";
        $("#t_body").html("");
        $("#t_body").append(str);

        $.ajax({ // 获取城市列表
            type : "GET",
            async: true,
            url : "user/user-server/listCity",
            headers: { // token
                'Authorization' : userInfo.token
            },
            success : function(result) {
                if (result!=null) {
                    var data = result.data;
                    var sel = document.getElementById("cityId");//获取下拉选对象
                    sel.options.length = 1; //清空
                    for(var i=0,l=data.length;i<l;i++){
                        var name = data[i].cityName;
                        var id = data[i].id;
                        var option = new Option(name,id);
                        if(id == cityId){
                            option.selected = true;
                        }
                        sel.options.add(option);
                    }
                    return;
                }
            }
        })
    }
    function deleteById(id) {
        if (confirm("您确定要删除该学校吗?")) {
            $.ajax({
                url: 'user/user-server/deleteSchool',
                method: 'Delete',
                headers: { // token
                    'Authorization': userInfo.token
                },
                dataType: "json",        //返回数据形式为json
                data: {"id": id},
                success: function (result) {
                    if (result.status == 200) {
                        alert("已删除");
                        init();
                    } else {
                        alert(result.msg);
                    }
                }
            })
        }
    }
    function update(id) {
        var schoolInfoParams = {
            id : id,
            schoolName : $("#newName").val(),
            cityId : $("#cityId").val()
        }
        if (confirm("您确定要修改该学校吗?")) {
            $.ajax({
                url: 'user/user-server/updateSchool',
                method: 'PUT',
                headers: { // token
                    'Authorization': userInfo.token
                },
                dataType: 'json',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(schoolInfoParams),
                success: function (result) {
                    if (result.status == 200) {
                        alert("修改成功");
                        init();
                    } else {
                        alert(result.msg);
                    }
                }
            })
        }
    }

    function insert() {
        var schoolInfoParams = {
            schoolName : $("#insertName").val(),
            cityId : $("#insertCityId").val()
        }
        $.ajax({
            url: 'user/user-server/insertSchool',
            method: 'POST',
            headers: { // token
                'Authorization': userInfo.token
            },
            dataType: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(schoolInfoParams),
            success: function (result) {
                if (result.status == 200) {
                    alert("创建成功");
                    init();
                    document.getElementById("insertName").value = "";
                } else {
                    alert(result.msg);
                }
            }
        })
    }

</script>

</html>